package de.hwrberlin.remo.diagram.properties;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;

import de.hwrberlin.remo.model.RemoObject;


public class DescriptionSection extends AbstractRemoPropertySection {
	
	private Text textDescription;
	private Label labelDescription;
	
	@Override
	public void createControls(Composite parent,
			TabbedPropertySheetPage aTabbedPropertySheetPage) {
		super.createControls(parent, aTabbedPropertySheetPage);
		
		Composite sectionClient = getWidgetFactory().createFlatFormComposite(parent);
		sectionClient.setLayout(new FormLayout());
		
		labelDescription = getWidgetFactory().createLabel(sectionClient, "Description:");

		FormData data = new FormData();
		data.top   = new FormAttachment(0, SectionConstants.TOP_MARGIN);
		data.left  = new FormAttachment(0, SectionConstants.LABEL_INDENT);
		data.width = SectionConstants.LABEL_WIDTH;
		labelDescription.setLayoutData(data);

		// -------------------------------------------------------------

		textDescription  = getWidgetFactory().createText(sectionClient, "", 
				SWT.MULTI | SWT.WRAP | SWT.V_SCROLL | SWT.BORDER);
		
		data = new FormData();
		data.top    = new FormAttachment(0, SectionConstants.TOP_MARGIN);
		data.left   = new FormAttachment(labelDescription);
		data.right  = new FormAttachment(80);
		data.height = SectionConstants.TEXTAREA_HEIGHT;
		textDescription.setLayoutData(data);
		
		textDescription.addFocusListener(descriptionFocusListener);
	}
	
	@Override
	public void refresh() {
		EObject eObject = getEObject();
	
		if(eObject != null && eObject instanceof RemoObject) {
			RemoObject exObject = (RemoObject) eObject;
			
			if(exObject.getDescription() != null) {
				textDescription.setText(exObject.getDescription());
			} else {
				textDescription.setText("");
			}
		} 
	}
	
	//////////////
	/* Listener */
	
	private FocusListener descriptionFocusListener = new FocusAdapter() {
		@Override
		public void focusLost(org.eclipse.swt.events.FocusEvent e) {
			editingHelper.setObjectDescription((RemoObject) getEObject(), textDescription.getText());
		};
	};
}